package com.jason.campusinn.view;

import android.app.Activity;
import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.PopupWindow;
import android.widget.TimePicker;

import com.yuandian.wanna.R;
import com.yuandian.wanna.utils.DateTimeUtil;
import com.yuandian.wanna.activity.navigationDrawer.OrderMeasureActivity;
import com.yuandian.wanna.utils.LogUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by yuandian on 2015/5/21.
 */
public class DateTimePickerPopupWindow extends PopupWindow implements View.OnClickListener {

    private View view;

    private Activity context;

    private DatePicker datePicker;

    private TimePicker timePicker;

    private Button btn_confirm;

    private String dateStr, timeStr, yymmdd;

    public DateTimePickerPopupWindow(Activity context) {

        this.context = context;
        //初始化页面
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.popup_datepicker, null);
        setContentView(view);
        initView(view);

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popupStyle);
        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0xB0FFFFFF);
        // 设置SelectPicPopupWindow弹出窗体的背景
//        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        this.update();
    }

    /**
     * @param view
     * @deprecated 初始化控件
     */
    private void initView(View view) {
        btn_confirm = (Button) view.findViewById(R.id.btn_popup_date_picker_confirm);
        btn_confirm.setOnClickListener(this);
        datePicker = (DatePicker) view.findViewById(R.id.popup_date_picker);
        //隐藏年份numberpicker
        ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
        //改变数字颜色

        Calendar calendar = Calendar.getInstance(Locale.CHINESE);
        int year = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

        Date date = new Date();
        DateFormat formatter = new DateFormat();
        yymmdd = DateTimeUtil.getYYYYMMDD(date, formatter);
        //OrderMeasureActivity.MEASURE_DATE = DateTimeUtil.getFormattedDateString(date, formatter);
        dateStr = DateTimeUtil.getMMDD(date, formatter);
        timeStr = DateTimeUtil.getkkmm(date, formatter);

        datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                LogUtil.d(year + monthOfYear + dayOfMonth + "");
            }
        });

       /* timePicker=(TimePicker)view.findViewById(R.id.popup_time_picker);
        //是否使用24小时制
        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

                timeStr = hourOfDay+":"+minute;
            }
        });*/
    }

    @Override
    public void onClick(View v) {
      //  OrderMeasureActivity.MEASURE_DATE = dateStr + " " + DateTimeUtil.getWeek(yymmdd);
//
        dismiss();
    }

}
